//
//  FinAuthAPI.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/7/8.
//

import Foundation
import Moya

enum FinAuthAPI {
    /// 获取 token
    /// - Parameters:
    ///   - type: 1=是注册 2=是修改密码
    ///   - mobile: 手机号
    case token(type: Int, mobile: String)
    /// 活体验证
    case liveness(token: String, mobile: String?)
    /// 对比结果
    case contrast(mobile: String, token: String, imageData: Data)
    /// 上传人脸文件
    case uploadFaceFile(mobile: String, imageBase64: String)
}

extension FinAuthAPI: TargetType {
    var baseURL: URL {
        switch self {
        case .contrast, .uploadFaceFile:
            return URL(string: SystemEnvManager.shared.freightForwardingUrl)!
        default:
            return URL(string: SystemEnvManager.shared.phpUrl)!
        }
    }
    
    var path: String {
        switch self {
        case .token:
            return "/api/FaceToken/faceToken"
        case .liveness:
            return "/api/FaceToken/faceLiveness"
        case .contrast:
            return "/api/Shoporder/faceContrast"
        case .uploadFaceFile:
            return "/api/Shoporder/add_file"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var task: Task {
        // 公共参数
        var params: [String: Any] = [:]
        
        switch self {
        case .token(let type, let mobile):
            params["type"] = type
            params["mobile"] = mobile
            
        case .liveness(let token, let mobile):
            params["biz_token"] = token
            params["mobile"] = mobile
            
        case .contrast(let mobile, let token, let imageData):
            params["mobile"] = mobile
            params["biz_token"] = token
            // 创建所有表单数据
            var multipartData = [MultipartFormData]()
            // 添加普通表单字段
            for (key, value) in params {
                if let valueData = "\(value)".data(using: .utf8) {
                    let formData = MultipartFormData(provider: .data(valueData), name: key)
                    multipartData.append(formData)
                }
            }
//            if let valueData = "\(token)".data(using: .utf8) {
//                multipartData.append(
//                    MultipartFormData(
//                        provider: .data(valueData),
//                        name: "biz_token"
//                    )
//                )
//            }
            
            // 添加文件数据
            multipartData.append(MultipartFormData(provider: .data(imageData), name: "image_ref1", fileName: "\(Date.sg.timestamp()).jpeg", mimeType: "image/jpeg"))
            
            return .uploadMultipart(multipartData)
        case .uploadFaceFile(let mobile, let imageBase64):
            params["mobile"] = mobile
            params["file"] = imageBase64
//            // 创建所有表单数据
//            var multipartData = [MultipartFormData]()
//            // 添加普通表单字段
//            for (key, value) in params {
//                if let valueData = "\(value)".data(using: .utf8) {
//                    let formData = MultipartFormData(provider: .data(valueData), name: key)
//                    multipartData.append(formData)
//                }
//            }
//            return .uploadMultipart(multipartData)
        }
        
        // 设置请求类型, 比如没有参数的请求、有参数的请求、上传文件、下载文件
        switch self {
        default:
            // 默认有请求参数
            if method == .get {
                // 在发起GET请求时，确保使用 URLEncoding.default 编码方式，这样参数会被正确添加到URL的查询字符串中，而不是作为请求体发送。
                return .requestParameters(parameters: params, encoding: URLEncoding.default)
            }
            return .requestParameters(parameters: params, encoding: JSONEncoding.default)
        }
    }
    
    
    var headers: [String: String]? {
        var headers: [String: String] = [:]
        headers["token"] = UserProfile.token
        headers["client"] = "app"
        headers["Content-type"] = "application/json;charset=utf-8"
        switch self {
        case .contrast:
            headers["Content-type"] = "multipart/form-data"
        default: break
        }
        headers["requestStartTime"] = String(CFAbsoluteTimeGetCurrent())
        return headers
    }
}
